﻿
Namespace storoomReader
    Public Module storoomReader

        Public Delegate Function convertDelegate(Of T)(ByVal input As bytes, ByRef output As T) As Boolean

        Public Function read(Of T)(ByVal key As String, ByRef output As T _
                                   , ByVal convert As convertDelegate(Of T)) As Boolean
            Dim buff As bytes = Nothing
            If storoom.seek(key) AndAlso storoom.read(key, buff) Then
                If Not convert(buff, output) Then
                    raiseError("cannot convert storoom output to " + GetType(T).ToString() _
                               , errorHandle.errorType.warning)
                    Return False
                Else
                    Return True
                End If
            Else
                Return False
            End If
        End Function

        Public Function read(ByVal key As String, ByRef output As Byte) As Boolean
            Return read(Of Byte)(key, output, AddressOf convertor.tobyte)
        End Function

        Public Function read(ByVal key As String, ByRef output As Int16) As Boolean
            Return read(Of Int16)(key, output, AddressOf convertor.toint16)
        End Function

        Public Function read(ByVal key As String, ByRef output As Int32) As Boolean
            Return read(Of Int32)(key, output, AddressOf convertor.toint32)
        End Function

        Public Function read(ByVal key As String, ByRef output As Int64) As Boolean
            Return read(Of Int64)(key, output, AddressOf convertor.toint64)
        End Function

        Public Function read(ByVal key As String, ByRef output As String) As Boolean
            Return read(Of String)(key, output, AddressOf convertor.toutf8string)
        End Function
    End Module
End Namespace
